﻿using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace Avalon.Core.MVC.Tempaltes
{
    internal static class TemplateReader
    {
        private static Assembly currrentAssembly { get { return typeof(TemplateReader).Assembly; } }
        private static Dictionary<string, string> templates;

        internal static string InputControlTemplate { get { return readResource("input_control_template"); } }
        internal static string InputHiddenTemplate { get { return readResource("input_hidden_template"); } }
        internal static string InputTextTemplate { get { return readResource("input_text_template"); } }
        internal static string InputMultilineTemplate { get { return readResource("input_multiline_template"); } }
        internal static string InputPasswordTemplate { get { return readResource("input_password_template"); } }
        internal static string InputDateTemplate { get { return readResource("input_date_template"); } }
        internal static string InputSelectTemplate { get { return readResource("input_select_template"); } }
        internal static string InputSelectOptionTemplate { get { return readResource("input_select_option_template"); } }
        
        private static string readResource(string key)
        {
            if (templates == null) templates = new Dictionary<string, string>();
            if (templates.ContainsKey(key)) return templates[key];

            using (var stream = currrentAssembly.GetManifestResourceStream(string.Format("Avalon.Core.MVC.Tempaltes.{0}.txt", key)))
            {
                using (var streamReader = new StreamReader(stream))
                {
                    templates[key] = streamReader.ReadToEnd();
                }
            }

            return templates[key];
        }
    }
}
